<?php
class Mwc_SystemPoint_FacebookController extends Mage_Core_Controller_Front_Action{
    public function indexAction(){
        $customer = Mage::getSingleton('customer/session');
        if($customer->isLoggedIn()){
            $collection = Mage::getModel('systempoint/userpoint')
                ->getCollection()
                ->addFieldToFilter('email', array('like' => "%{$customer->getUsername()}"));
            $userpoint = $collection->getFirstItem();

            $setting = Mage::getModel('systempoint/setting')->load(1);
            $userpoint->setSharedPoint($setting->getSharePoint());
            if($setting->getSharePoint() > $userpoint->getUserPoint()){
                $userpoint->setUserPoint($setting->getSharePoint());
            }
            $userpoint->save();

            $logCollection = Mage::getModel('systempoint/log')
                ->getCollection()
                ->addFieldToFilter('user_email', array('like' => "%{$customer->getUsername()}"))
                ->addFieldToFilter('title', array('eq' => "{$setting->getShareText()}"));
            $log = $logCollection->getFirstItem();
            $log->setUserEmail($customer->getUsername());
            $log->setTitle($setting->getShareText());
            $log->setPoint($setting->getSharePoint());
            $log->setLogDate(date('Y-m-d H:i:s', mktime()));
            $log->save();

            /*
            $mem = Mage::getModel('systempoint/membership')->loadHighestPoint();
            if($mem->getNumberOfPoint() < $setting->getSharePoint()){
                $vipl = Mage::getModel('systempoint/viplist');
                $vipl->setEmail($customer->getUsername());
                $vipl->setDate(date('Y-m-d H:i:s', mktime()));
                $vipl->save();
            }
            */

            echo 1;
        }
        else{
            echo 0;
        }
    }
}